package com.example.home.metro.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.home.R;
import com.example.home.metro.adapter.PathMessageDetailAdapter;
import com.example.home.metro.bean.PathMessageBean;
import com.example.home.metro.bean.PathMessageDetailBean;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

public class PathMessageDetail extends AppCompatActivity {

    private PathMessageBean.DataBean dataBean;
    private ImageView back;
    private TextView title;
    private TextView first;
    private TextView end;
    private TextView shengTime;
    private TextView shengZhan;
    private TextView shengKm;
    private TextView now;
    private RecyclerView recyclerView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_path_message_detail);
        Intent intent = getIntent();
        String pathId = intent.getStringExtra("pathId");
        Gson gson = new Gson();
        dataBean = gson.fromJson(pathId, PathMessageBean.DataBean.class);
        initView();
        getPathMessage();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    private void getPathMessage() {
        OKHttpTool.doNetRequest("prod-api/api/metro/line/"+dataBean.getLineId(),
                OKHttpTool.GET,
                PathMessageDetailBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        PathMessageDetailBean pathMessageDetailBean = (PathMessageDetailBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                first.setText("首发站："+pathMessageDetailBean.getData().getFirst());
                                end.setText("终点站："+pathMessageDetailBean.getData().getEnd());
                                shengZhan.setText("剩"+pathMessageDetailBean.getData().getStationsNumber()+"站");
                                shengKm.setText("剩"+pathMessageDetailBean.getData().getKm()+"km");
                                shengTime.setText("剩余时间："+pathMessageDetailBean.getData().getRemainingTime());
                                now.setText(pathMessageDetailBean.getData().getRunStationsName());
                                bindPath(pathMessageDetailBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindPath(PathMessageDetailBean pathMessageDetailBean) {
        PathMessageDetailAdapter pathMessageDetailAdapter = new PathMessageDetailAdapter(PathMessageDetail.this, pathMessageDetailBean.getData());
        LinearLayoutManager layoutManager = new LinearLayoutManager(PathMessageDetail.this, LinearLayoutManager.HORIZONTAL, false);
        recyclerView.setAdapter(pathMessageDetailAdapter);
        recyclerView.setLayoutManager(layoutManager);
        pathMessageDetailAdapter.updateData(pathMessageDetailBean.getData());
    }


    private void initView() {


        back = findViewById(R.id.back);
        title = findViewById(R.id.title);
        first = findViewById(R.id.first);
        end = findViewById(R.id.end);
        shengTime = findViewById(R.id.sheng_time);
        shengZhan = findViewById(R.id.sheng_zhan);
        shengKm = findViewById(R.id.sheng_km);
        recyclerView = findViewById(R.id.recyclerView);
        now = findViewById(R.id.now);

        title.setText(dataBean.getLineName());


        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PathMessageDetail.this.finish();
            }
        });
    }
}